package ServiceImp;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import DAO.CategoryDAO;
import DAO.CustomerDAO;
import DAO.CustomerTypeDAO;
import DAO.ImageDAO;
import DAO.ProductDAO;
import DAO.ProductDetailDAO;
import DAO.UserDAO;
import ModelBean.NodeCategory;
import ModelBean.ProductBean;
import POJO.Category;
import POJO.Customer;
import POJO.CustomerType;
import POJO.Image;
import POJO.Product;
import POJO.ProductDetail;
import POJO.User;
import Service.AdminService;

@Service("serviceAdmin")
@Transactional
public class AdminServiceImp implements AdminService {

	@Autowired
	CategoryDAO categoryDAO;
	
	@Autowired 
	ProductDAO productDAO;
	
	@Autowired 
	ProductDetailDAO productDetailDAO;
	
	@Autowired 
	ImageDAO imageDAO;

	@Autowired 
	CustomerDAO custonmerDAO;
	
	@Autowired 
	CustomerTypeDAO custonmerTypeDAO;
	
	public List<Category> getListCategory() {
		return categoryDAO.getAll();
	}

	public Category getCategoryById(int id) {
		if (id == 0 || id == -1) {
			return new Category();
		}
		return categoryDAO.get(id);
	}

	public void saveCategory(Category category) {
		Category cateTemp = categoryDAO.get(category.getCategoryId());
		
		if(cateTemp.getParentId() != category.getParentId())
		{
			
			
			
			NodeCategory root = createRoot();
			
			
			NodeCategory nodeTemp = new NodeCategory();
			
			for (NodeCategory i : root.childs) {
				if (i.value.getCategoryId() == cateTemp.getCategoryId())
					nodeTemp = i;
				else {
					for (NodeCategory ii : i.childs)
						if (ii.value.getCategoryId() == cateTemp.getCategoryId())
							nodeTemp = ii;
						else {
							for (NodeCategory iii : ii.childs)
								if(iii.value.getCategoryId() == cateTemp.getCategoryId())
									nodeTemp = iii;
								else {
									for(NodeCategory iiii : iii.childs)
										if(iiii.value.getCategoryId() == cateTemp.getCategoryId())
											nodeTemp = iiii;
								}
						}
				}
			}
			
			//System.out.println(nodeTemp.value.getCategoryName());
			cateTemp.setParentId(category.getParentId());
			Category cateParent = new Category();
			int priority = 0;
			if(category.getParentId() != -1)
			{
				cateParent = categoryDAO.get(category.getParentId());	
				priority = cateParent.getPriority() + 1;
			}
				
			
			List<NodeCategory> lastTemp = new ArrayList<NodeCategory>();
			lastTemp.add(nodeTemp);
			int count = 0;
			
			while (lastTemp.size() > 0) {
				for (NodeCategory nodeCategory : lastTemp) {		
										
					System.out.println(category.getParentId());
					if(count == 0 && category.getParentId() != -1)
					{
						nodeCategory.value.setParentId(cateParent.getCategoryId());
						nodeCategory.value.setPriority(priority);
					}if(count == 0 && category.getParentId() == -1)
					{
						nodeCategory.value.setParentId(-1);
						nodeCategory.value.setPriority(0);
					}else
					{
						nodeCategory.value.setPriority(priority);						
					}		
				}

				
				List<NodeCategory> listTmp = new ArrayList<NodeCategory>();
				for (NodeCategory cc : lastTemp) {
					for (NodeCategory ccc : cc.childs) {
						listTmp.add(ccc);
					}
				}

				lastTemp.clear();
				lastTemp = listTmp;
				count++;
				priority++;
			}
			
			
		}
		
		//categoryDAO.update(category);
	}

	public void addCategory(Category category) {
		Category temp = new Category();

		temp.setCategoryName(category.getCategoryName());
		temp.setDescription(category.getDescription());
		temp.setStatus(category.getStatus());
		temp.setTag(category.getTag());

		temp.setParentId(category.getParentId());
		if (category.getParentId() == -1)
			temp.setPriority(0);
		else {
			temp.setPriority(categoryDAO.get(category.getParentId())
					.getPriority() + 1);
		}

		categoryDAO.create(temp);
	}

	public void deleteCategory(Category category) {
		resetPriority(category);
		categoryDAO.deleteById(category.getCategoryId());
	}

	public void resetPriority(Category category) {

		NodeCategory root = createRoot();
		
		
		NodeCategory nodeTemp = new NodeCategory();
		Category cateTemp = categoryDAO.get(category.getCategoryId());
		for (NodeCategory i : root.childs) {
			if (i.value.getCategoryId() == cateTemp.getCategoryId())
				nodeTemp = i;
			else {
				for (NodeCategory ii : i.childs)
					if (ii.value.getCategoryId() == cateTemp.getCategoryId())
						nodeTemp = ii;
					else {
						for (NodeCategory iii : ii.childs)
							if(iii.value.getCategoryId() == cateTemp.getCategoryId())
								nodeTemp = iii;
							else {
								for(NodeCategory iiii : iii.childs)
									if(iiii.value.getCategoryId() == cateTemp.getCategoryId())
										nodeTemp = iiii;
							}
					}
			}
		}
		
		//System.out.println(nodeTemp.value.getCategoryName());

		List<NodeCategory> lastTemp = new ArrayList<NodeCategory>();
		lastTemp.add(nodeTemp);
		int count = 0;
		
		while (lastTemp.size() > 0) {
			for (NodeCategory nodeCategory : lastTemp) {
				int priority = nodeCategory.value.getPriority();
				if(count != 0)
					nodeCategory.value.setPriority(priority - 1);
				
				
				if(cateTemp.getPriority() != 0 && count == 1)
				{
					nodeCategory.value.setParentId(cateTemp.getParentId());
					System.out.println(nodeCategory.value.getCategoryName());
				}
					
			}

			
			List<NodeCategory> listTmp = new ArrayList<NodeCategory>();
			for (NodeCategory cc : lastTemp) {
				for (NodeCategory ccc : cc.childs) {
					listTmp.add(ccc);
				}
			}

			lastTemp.clear();
			lastTemp = listTmp;
			count++;
		}
	}

	private NodeCategory createRoot() {
		NodeCategory root = new NodeCategory();

		int nLevel = categoryDAO.getMaxLevel() + 1;
		List<Category> current = categoryDAO.getCategories(0);

		for (Category c : current) {
			root.addChild(c);
		}

		List<NodeCategory> last = new ArrayList<NodeCategory>();
		last.add(root);

		for (int i = 1; i < nLevel; i++) {
			current = categoryDAO.getCategories(i);

			for (NodeCategory node : last) {
				for (Category cat : current) {
					for (NodeCategory n : node.childs)
						if (n.value.getCategoryId() == cat.getParentId()) {
							n.addChild(cat);
						}
				}
			}

			List<NodeCategory> tmp = new ArrayList<NodeCategory>();
			for (NodeCategory cc : last) {
				for (NodeCategory ccc : cc.childs) {
					tmp.add(ccc);
				}
			}

			last.clear();
			last = tmp;
		}
		return root;
	}

	public List<Product> getListProducts() {
		return productDAO.getAll();
	}

	public Product getProducById(int productId) {		
		return productDAO.get(productId);
	}

	public void saveProduct(Product product) {
		productDAO.update(product);
		
	}

	public void deleteProduct(Product product) {
		productDAO.deleteById(product.getProductId());		
	}

	public List<ProductDetail> getListProductDetail(int productId) {
		
		return productDetailDAO.getProductDetailsById(productId);
	}

	public Product getProductById(int productId) {
		return productDAO.get(productId);
	}

	public ProductDetail getProductDetail(int productDetailId) {
		return productDetailDAO.get(productDetailId);
	}

	public List<Image> getListImage() {
		return imageDAO.getAll();
	}

	public void saveProductDetail(ProductDetail productDetail) {
		
		ProductDetail product = productDetailDAO.get(productDetail.getProductDetailId());
		product.setPrice(productDetail.getPrice());
		product.setOldPrice(productDetail.getOldPrice());
		
		product.setImages(productDetail.getImages());
		for (Image image : productDetail.getImages()) {			
			product.getImages().add(image);
		}		
		
		
		
	}

	public void createProduct(Product product) {
		productDAO.create(new Product(product.getProductName(), product.getTag(), product.getDescription(), product.getDetailInfo(), product.getStatus(), product.getDeleted(), product.getBrand(), product.getCategories(), product.getDateCreate()));		
	}

	public void createProductDetail(ProductDetail productDetail) {
		productDetailDAO.create(new ProductDetail(productDetail.getCount(), productDetail.getPrice(), productDetail.getOldPrice(), productDetail.getDeleted(), 
				productDAO.get(1)
				, productDetail.getTypeDetails(), productDetail.getImages(), imageDAO.get(1)));		
	}

	public List<Customer> getListAccount() {
		
		return custonmerDAO.getAll();
	}

	public Customer getAccountById(int customerId) {
		// TODO Auto-generated method stub
		return custonmerDAO.get(customerId);
	}

	public void saveAccount(Customer cus) {
		
		Customer c = custonmerDAO.get(cus.getCustomerId());
		
		
		c.setMinPurchase(cus.getMinPurchase());
		c.setMaxPurchase(cus.getMaxPurchase());
		System.out.println(cus.getMaxPurchase());
		c.setFirstName(cus.getFirstName());
		c.setLastName(cus.getLastName());
		c.setAddress(cus.getAddress());
		c.setPhone(cus.getPhone());
		c.setEmail(cus.getEmail());
		//custonmerDAO.update(cus);		
	}
}
